from flask import Flask,request
import os
import json
import os
from pathlib import Path
from predict import predict as main_LSTM
root=Path.cwd()
app = Flask(__name__)

@app.route("/LSTM",methods=["POST"])
def runXAJ():
    tdata = request.json

    jdata = json.dumps(tdata)
    with open("LSTM_data_check.json","w") as fl:
        print(jdata,file=fl)

    if isinstance(tdata,dict):
        data=tdata
    else:
        jdata = json.dumps(tdata)
        # with open("config_LSTM.json","w",encoding="utf8") as fl:
        #     print(jdata,file=fl)

        # with open("config_LSTM.json","r",encoding="utf8") as fl:
        #     tdata=json.load(fl)
        data=tdata

    info={}
    info["msg"]="beta run"
    code=200
    status={}
    rst = []
    try:
        rst=main_LSTM(data)
        status["LSTM"]="success"
    except:
        code=1003
        status["LSTM"]="failed"
    print(f"LSTM:{status['LSTM']}")

    info["msg"]=status
    info["data"]=rst
    info["code"]=code
    return info



@app.route("/tst")
def hello():
    return "hello world!"


if __name__=="__main__":
    # subprocess.Popen(["java", "-jar", f"{root}\\ddrm-0.0.1-SNAPSHOT.jar"], shell=True)
    # print("run model")
    app.run(debug=True,host="0.0.0.0",port=61001)
